import { Component, OnInit,Output,EventEmitter } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import { BackendService } from '../backend.service';
// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
import * as moment from 'moment';
// // tslint:disable-next-line:no-duplicate-imports
// import {default as _rollupMoment} from 'moment';
 
// const moment = _rollupMoment || _moment;
// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = {
  parse: {
    dateInput: 'YYYY-MM-DD',  // 表示input中输入的格式，moment将按此格式解析内容
  },
  display: {
    dateInput: 'YYYY-MM-DD',  // 显示格式，monent将日期按此格式呈现给用户
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  },
};

@Component({
  selector: 'app-findfile',
  templateUrl: './findfile.component.html',
  styleUrls: ['./findfile.component.css'],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    {
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    },
 
    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
  ],
})
export class FindfileComponent implements OnInit {

  constructor(private backend: BackendService) { }
  fileName:string;
  fileDate = new FormControl({value: null,disabled: true});
  fileKeyword:string;
  fileNo:string;
  fileJg:string;
  fileYwlx:string;
  searchTxt:string;
  links:Array<Object>=[];
  ngOnInit(): void {
  }
  @Output() clickFindedfile:EventEmitter<Object> =new EventEmitter<Object>();
  @Output() clickSearchfile:EventEmitter<Object> =new EventEmitter<Object>();
  searhFileFromTxt(){
    console.log("searhFileFromTxt",this.searchTxt);
    this.links=[];
    this.clickSearchfile.emit({txt:this.searchTxt});
    this.backend.searchFile({txt:this.searchTxt}).subscribe(result=>{
      console.log(result);
      if(result.ok){
        this.links=result.data;
      }
    });
  }
  searhFileFromExtinfo(){
    console.log("精确查找",this.fileName);
    let fileNo;
    this.searchTxt="";
    this.links=[];
    if(this.fileNo){
      fileNo=this.fileNo.replace("【","[").replace("】","]").replace("（","[").replace("）","]").replace("(","[").replace(")","]").replace(/\s/g,"");
    }
    let param={
      fileName:this.fileName,
      fileDate:this.fileDate.value==null?null:this.fileDate.value.format('YYYY-MM-DD'),
      fileKeyword:this.fileKeyword,
      fileNo:fileNo,
      fileJg:this.fileJg,
      fileYwlx:this.fileYwlx
    }
    this.clickSearchfile.emit({txt:this.searchTxt});
    this.backend.searhFileFromExtinfo(param).subscribe(result=>{
      console.log(result);
      if(result.ok){
        this.links=result.data;
      }
    });
  }
  onRowClicked(link:Object){
    console.log(link);
    this.clickFindedfile.emit({link:link,searchTxt:this.searchTxt});
  }
}
